package com.xnck.example.sbtest.controller;

import com.xnck.example.sbtest.entity.AppInfo;
import com.xnck.example.sbtest.enums.ResponseCode;
import com.xnck.example.sbtest.model.ResponseJson;
import com.xnck.example.sbtest.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("app")
public class AppController {

    @Autowired
    private AppService appService;

    @RequestMapping("/count")
    public ResponseJson getAppCount() {
        int total = appService.getCount();
        return new ResponseJson(ResponseCode.SUCCESS.getCode(), "", total);
    }

    @RequestMapping("/apps/{pageno}/{pagesize}")
    public ResponseJson getApps(@PathVariable(value = "pageno", required = false)Integer pageNo,
                                @PathVariable(value = "pagesize", required = false)Integer pageSize) {
        if (null == pageNo) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 15;
        }
        List<AppInfo> apps = appService.getsByPage(pageNo, pageSize);
        return new ResponseJson(ResponseCode.SUCCESS.getCode(), "", apps);
    }
}
